//
//  ListDetailViewController.m
//  CheckList
//
//  Created by chen on 15/8/5.
//  Copyright (c) 2015年 ZhangXM. All rights reserved.
//

#import "ListDetailViewController.h"
#import "Checklist.h"

@interface ListDetailViewController ()

@end

@implementation ListDetailViewController

//判断内容是否为空，从而确定是Add还是Edit
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.checklistToEdit != nil) {
        self.title = @"Edit Checklist";
        self.textField.text = self.checklistToEdit.name;
        self.doneBarButton.enabled = YES;
    }
}
// 键盘
- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.textField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancel:(id)sender{
    [self.delegate listDetailViewControllerDidCancel:self];

}

- (IBAction)done:(UIBarButtonItem *)sender {
    if(self.checklistToEdit == nil){
        Checklist *checklist = [[Checklist alloc]init];
        checklist.name = self.textField.text;
        
        [self.delegate listDetailViewController:self didFinshAddingChecklist:checklist];
    }else{
        self.checklistToEdit.name = self.textField.text;
        
        [self.delegate listDetailViewController:self didFinshEditingChecklist:self.checklistToEdit];
    }

}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.doneBarButton.enabled = ([newText length]>0);
    return YES;
}


@end
